// pages/Census/index.js
const Controllers = require('../controllers/ControllerCensus.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar: app.globalData.tabbar,
    tabPage: app.globalData.tabPage,
    head: [
      {
        title: '资金统计',
        detail: '查看明细',
        url: '/pages/Census/fund-detail/index',
        status: 1,
        childs: [
          {
            laber: '总收益',
            value: '',
            key: 'totalMoney',
            unit: '¥',
            url: '',
            className: ''
          },
          {
            laber: '租金收益',
            value: '1000.25',
            key: 'rent',
            unit: '¥',
            url: '',
            className: 'fontColor'
          },
          {
            laber: '云商品收益',
            value: '1000.25',
            key: 'cloud',
            unit: '¥',
            url: '',
            className: 'fontColor'
          },
          {
            laber: '员工提成',
            value: '1000.25',
            key: 'agentStaffIncome',
            unit: '¥',
            url: '',
            className: 'fontColor'
          }
        ]
      },
      {
        title: '客户统计',
        detail: '',
        url: '',
        status: 1,
        childs: [
          {
            laber: '客户数量',
            value: '',
            key: 'shopNum',
            url: '/pages/Census/customer/list',
            className: ''
          },
          {
            laber: '租金收益',
            value: '1000.25',
            key: 'rent',
            unit: '¥',
            url: '/pages/Census/customer/profit?active=0',
            className: 'fontColor'
          },
          {
            laber: '云商品收益',
            value: '1000.25',
            key: 'cloud',
            unit: '¥',
            url: '/pages/Census/customer/profit?active=1',
            className: 'fontColor'
          }
        ]
      },
      {
        title: '代理商统计',
        detail: '',
        url: '',
        status: 0,
        childs: [
          {
            laber: '代理商数量',
            value: '1000',
            key: 'sonAgentNum',
            unit: '',
            url: '/pages/Census/agent/list',
            className: ''
          },
          {
            laber: '下级租金收益',
            value: '',
            key: 'sonAgentRent',
            unit: '',
            url: '',
            url: '/pages/Census/agent/profit?active=0',
            className: 'fontColor'
          },
          {
            laber: '下级云商品收益',
            value: '',
            key: 'sonAgentCloud',
            unit: '',
            url: '/pages/Census/agent/profit?active=1',
            className: 'fontColor'
          }
        ]
      },
      {
        title: '员工统计',
        detail: '查看明细',
        url: '/pages/Index/Staff/index',
        status: 1,
        childs: [
          {
            laber: '员工数量',
            value: 0,
            key: 'agentStaffNum',
            unit: '',
            url: '/pages/Index/Staff/index',
            className: ''
          },
          {
            laber: '提成总金额',
            value: 0,
            key: 'agentStaffIncome',
            unit: '¥',
            url: '',
            className: 'fontColor'
          },
          {
            laber: '员工提现总额',
            value: 0,
            key: 'agentStaffCommission',
            unit: '¥',
            url: '',
            className: 'fontColor'
          }
        ]
      }
    ],
    statistics: {}
  },
  dataStatistics() {
    var that = this;
    wx.showLoading();
    Controllers.dataStatistics({}).then(res => {
      console.log('dataStatistics--res==', res);
      const { code, data, desc } = res;
      var head = that.data.head;
      if ([0,1].includes(data.type)) {
        head[2].status = 1;
        // 代理商统计》代理商数量；
        data.sonAgentNum = parseInt(data.sonAgent.num);
        // 代理商统计》下级租金收益；
        data.sonAgentRent = parseFloat(data.sonAgent.rent).toFixed(2);
        // 代理商统计》下级云商品收益；
        data.sonAgentCloud = parseFloat(data.sonAgent.cloud).toFixed(2);
      } else {
        head[2].status = 0;
      }
      // 资金统计》总收益；
      data.totalMoney = parseFloat(data.rent + data.cloud + data.agentStaffCommission).toFixed(2);

      that.setData({
        head,
        statistics: data
      })
      wx.hideLoading();
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      tabbar: app.globalData.tabbar,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var agentIndexData = wx.getStorageSync('agentIndex');
    console.log('onShow--agentIndexData==', agentIndexData);
    
    that.setData({
      agentIndexData
    })
    that.dataStatistics();
  },
  tabChange(e) {
    app.tabChange(e);
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})